package com.gui.service;

import com.gui.DTO.LimitDTO;
import com.gui.DTO.QuestionDTO;
import com.gui.mapper.QuestionMapper;
import com.gui.mapper.UserMapper;
import com.gui.model.Question;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * LimitPageSevice
 *
 * @author guigu
 * @QQ 1170258867
 * @date 2020/6/1
 * @description
 */
@Component
public class LimitPageSevice {

    private LimitDTO limitDTO;

    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserMapper userMapper;

    /*所有得到数据条数*/
    public int getCount() {
        return questionMapper.getCountquestion();
    }

    /*得到一个人的帖子条数*/
    public int getCount(int creator) {
        return questionMapper.getPersonalCountquestion(creator);
    }

    /*所有有多少页，默认每页五个*/
    public int getallPages(int countpages) {
        return (countpages + 4) / 5;
    }

    /*主页建造者建造分页类*/
    public LimitDTO Bulider(LimitDTO limitDTO, int page) {
        this.limitDTO = limitDTO;
        limitDTO.setAllPage(getallPages(getCount()));
        limitDTO.setNowPage(page);
        limitDTO.setQuestionDTOS(getQuestions(page));
        limitDTO.setFrist(getfirst(page));
        limitDTO.setEnd(getend(page));
        limitDTO.setPre(getpre(page));
        limitDTO.setSub(getsub(page));
        limitDTO.setPages(getPages(page));
        return this.limitDTO;
    }

    /*个人中心建造者建造分页类*/
    public LimitDTO Bulider(LimitDTO limitDTO, int page, int creator) {
        this.limitDTO = limitDTO;
        limitDTO.setAllPage(getallPages(getCount(creator)));
        limitDTO.setNowPage(page);
        limitDTO.setQuestionDTOS(getQuestions(page, creator));
        limitDTO.setFrist(getfirst(page));
        limitDTO.setEnd(getend(page, creator));
        limitDTO.setPre(getpre(page));
        limitDTO.setSub(getsub(page, creator));
        limitDTO.setPages(getPages(page, creator));
        return this.limitDTO;
    }

    /*主页的建造者分页*/
    private List<QuestionDTO> getQuestions(int page) {
        List<QuestionDTO> list = new ArrayList<>();
        int pre = (page - 1) * 5;
        List<Question> limitQuestion = questionMapper.getLimitQuestion(pre);
        for (Question question : limitQuestion) {
            QuestionDTO questionDTO = new QuestionDTO();
            BeanUtils.copyProperties(question, questionDTO);
            questionDTO.setUser(userMapper.selectById(question.getCreator()));
            list.add(questionDTO);
        }
        return list;
    }

    /*个人中心的帖子*/
    private List<QuestionDTO> getQuestions(int page, int creator) {
        List<QuestionDTO> list = new ArrayList<>();
        int pre = (page - 1) * 5;
        List<Question> limitQuestion = questionMapper.getLimitQuestionPerson(page, creator);
        for (Question question : limitQuestion) {
            QuestionDTO questionDTO = new QuestionDTO();
            BeanUtils.copyProperties(question, questionDTO);
            questionDTO.setUser(userMapper.selectById(question.getCreator()));
            list.add(questionDTO);
        }
        return list;
    }

    /*首页按钮是否存在*/
    private boolean getfirst(int page) {
        return page < 4 ? false : true;
    }

    private boolean getend(int page) {
        return page > getallPages(getCount()) - 2 ? false : true;
    }

    private boolean getend(int page, int creator) {
        return page > getallPages(getCount(creator)) - 2 ? false : true;
    }

    private boolean getpre(int page) {
        return page == 1 ? false : true;
    }

    private boolean getsub(int page) {
        return page >= getallPages(getCount()) ? false : true;
    }

    private boolean getsub(int page, int creator) {
        return page >= getallPages(getCount(creator)) ? false : true;
    }

    private List<Integer> getPages(int page) {
        List<Integer> list = new ArrayList<>();
        list.add(page);
        int r = page;
        int l = page;
        int allPages = getallPages(getCount());
        while (list.size() < 4&&l <= allPages) {
            r--;
            l++;
            if (r > 0 && list.size() < 4) {
                list.add(0, r);
            }
            if (l <= allPages && list.size() < 4) {
                list.add(l);
            }
        }
        return list;
    }

    private List<Integer> getPages(int page, int creator) {
        List<Integer> list = new ArrayList<>();
        list.add(page);
        int r = page;
        int l = page;
        int allPages = getallPages(getCount(creator));
        while (list.size() < 4&&l <= allPages) {
            r--;
            l++;
            if (r > 0 && list.size() < 4) {
                list.add(0, r);
            }
            if (l <= allPages&& list.size() < 4) {
                list.add(l);
            }
        }
        return list;
    }


}
